package com.zmx.doctor.view.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.netease.nim.avchatkit.AVChatKit;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.auth.AuthService;
import com.netease.nimlib.sdk.auth.LoginInfo;
import com.zmx.doctor.BaseActivity;
import com.zmx.doctor.MainActivity;
import com.zmx.doctor.NimSDKOptionConfig;
import com.zmx.doctor.R;
import com.zmx.doctor.fragment.FourFragment;
import com.zmx.doctor.utils.AllUtil;
import com.zmx.doctor.view.util.MySharedPreferences;
import com.zmx.doctor.wangyiyun.DemoCache;
import com.zmx.doctor.wangyiyun.saveData.DataSaveToLocal;

public class WelcomeActivity extends BaseActivity {

    @Override
    protected int getLayoutId() {
        return R.layout.activity_welcome;
    }

    @Override
    protected void initViews() {

        //判断是否登录了
        String token = MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, "");

        if (AllUtil.isNotNull(token)) {

//            doLogin(getLoginInfo());

            //充钱就删的
            Intent intent = new Intent();
            intent.setClass(mActivity, MainActivity.class);
            startActivity(intent);
            finish();

        }else {

            Intent intent = new Intent();
            intent.setClass(mActivity, LoginActivity.class);
            startActivity(intent);
            finish();
        }

    }

    private void doLogin(LoginInfo loginInfo){

        RequestCallback<LoginInfo> callback = new RequestCallback<LoginInfo>() {
            @Override
            public void onSuccess(LoginInfo loginInfo) {

                DemoCache.setContext(getApplicationContext());
                AVChatKit.setAccount(loginInfo.getAccount());
                //跳转到拨打界面
                Intent intent = new Intent();
                intent.setClass(mActivity, MainActivity.class);
                startActivity(intent);
                finish();

            }

            @Override
            public void onFailed(int i) {
                Toast.makeText(mActivity, "登陆失败，错误码："+i, Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                intent.setClass(mActivity, LoginActivity.class);
                startActivity(intent);
                finish();
            }

            @Override
            public void onException(Throwable throwable) {

                Toast.makeText(mActivity, "登陆异常，" + throwable.toString(), Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                intent.setClass(mActivity, LoginActivity.class);
                startActivity(intent);
                finish();

            }
        };
        NIMClient.getService(AuthService.class).login(loginInfo).setCallback(callback);
    }


    private DataSaveToLocal dataSaveToLocal = new DataSaveToLocal();
    //取出本地保存的用户信息
    private LoginInfo getLoginInfo(){

        String loginData = dataSaveToLocal.readDataToLocal(NimSDKOptionConfig.getAppCacheDir(getApplicationContext())+"logininfo.txt");
        if(!loginData.isEmpty()){
            int index1 = loginData.indexOf(",");
            String accid = loginData.substring(0,index1);
            String token = loginData.substring(index1+1,loginData.length());
            LoginInfo loginInfo = new LoginInfo(accid,token);
            return loginInfo;
        }else {
            return null;
        }

    }
}
